//
//  TheOrderDetailsViewController.m
//  BaseFrame
//
//  Created by 吕金状 on 2017/8/15.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "ShopTheOrderDetailsViewController.h"
#import "ShopTheOrderDetailsTableViewCell.h"

@interface ShopTheOrderDetailsViewController ()<UITableViewDelegate , UITableViewDataSource>

Strong UIView *headView;
Strong UILabel *titleLabel;
Strong UIView *footView;
Strong UILabel *phoneNumberLabel;
Strong UITableView *tableView;
Strong NSMutableArray *dataArray; /**<*/
Strong UIImageView *titleImageView;
Strong UILabel *moneyLabel;
Strong UILabel *timeLabel;
Strong UILabel *theOrderNumber;
Strong UILabel *theOrderNumberLabel;
Strong UILabel *logisticsInformationLabel; // 物流信息
Strong UIButton *toApplyForARefundButton; //申请退款

@end

@implementation ShopTheOrderDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - init

- (UIButton *)toApplyForARefundButton{
    if (!_toApplyForARefundButton) {
        _toApplyForARefundButton = [[UIButton alloc]init];
        _toApplyForARefundButton.titleLabel.font = DEF_FontSize_14;
        _toApplyForARefundButton.backgroundColor = getColor(mainColor);
        [_toApplyForARefundButton setTitle:@"申请退款" forState:UIControlStateNormal];
        [_toApplyForARefundButton setTitleColor:getColor(@"ffffff") forState:UIControlStateNormal];
        [_toApplyForARefundButton addTarget:self action:@selector(toApplyForARefundButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _toApplyForARefundButton;
}

- (UILabel *)theOrderNumberLabel{
    if (!_theOrderNumberLabel) {
        _theOrderNumberLabel  = [[UILabel alloc]init];
        _theOrderNumberLabel.font = DEF_FontSize_14;
        _theOrderNumberLabel.textColor = getColor(textColor);
        _theOrderNumberLabel.textAlignment = NSTextAlignmentRight;
        _theOrderNumberLabel.text = @"3253454213123";
    }
    return _theOrderNumberLabel;
}

- (UILabel *)logisticsInformationLabel{
    if (!_logisticsInformationLabel) {
        _logisticsInformationLabel  = [[UILabel alloc]init];
        _logisticsInformationLabel.font = DEF_FontSize_14;
        _logisticsInformationLabel.textColor = getColor(textColor);
        _logisticsInformationLabel.attributedText = [self getAttributeName];
    }
    return _logisticsInformationLabel;
}

- (NSAttributedString *)getAttributeName{
    NSMutableAttributedString *attStr = [NSMutableAttributedString new];
    NSAttributedString *place = [[NSAttributedString alloc]initWithString:@"(本服务由XXX物流公司提供)" attributes:@{NSFontAttributeName:DEF_FontSize_12}];
    NSTextAttachment *seeAtt = [NSTextAttachment new];
    seeAtt.bounds = CGRectMake(0, -2 / WIDTH_5S_SCALE, 10 / WIDTH_5S_SCALE, 13 / WIDTH_5S_SCALE);
    seeAtt.image = [UIImage imageNamed:@"ShopIsIntroduced"];
    [attStr appendAttributedString:[NSAttributedString attributedStringWithAttachment:seeAtt]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"  %@",@"物流信息"]]];
    [attStr appendAttributedString:place];
    return attStr;
}

- (UILabel *)phoneNumberLabel{
    if (!_phoneNumberLabel) {
        _phoneNumberLabel  = [[UILabel alloc]initWithFrame:CGRectMake(100 / WIDTH_5S_SCALE, 0, SCREEN_WIDTH - 110 / WIDTH_5S_SCALE, 28 / WIDTH_5S_SCALE)];
        _phoneNumberLabel.font = DEF_FontSize_14;
        _phoneNumberLabel.textColor = getColor(mainColor);
        _phoneNumberLabel.textAlignment = NSTextAlignmentRight;
        _phoneNumberLabel.text = @"15542914939";
    }
    return _phoneNumberLabel;
}

- (UIView *)headView{
    if (!_headView) {
        _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 204 / WIDTH_5S_SCALE + 2.5)];
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 28 / WIDTH_5S_SCALE)];
        
        UILabel *leftLabel  = [[UILabel alloc]initWithFrame:CGRectMake(10 / WIDTH_5S_SCALE, 0, 200, 28 / WIDTH_5S_SCALE)];
        leftLabel.font = DEF_FontSize_14;
        leftLabel.textColor = getColor(mainColor);
        NSMutableAttributedString *attStr1 = [NSMutableAttributedString new];
        NSTextAttachment *seeAtt1 = [NSTextAttachment new];
        seeAtt1.bounds = CGRectMake(0, - 2, 10 / WIDTH_5S_SCALE, 12 / WIDTH_5S_SCALE);
        seeAtt1.image = [UIImage imageNamed:@"phone"];
        [attStr1 appendAttributedString:[NSAttributedString attributedStringWithAttachment:seeAtt1]];
        [attStr1 appendAttributedString:[[NSAttributedString alloc] initWithString:@" 联系商家"]];
        leftLabel.attributedText = attStr1;
        [bgView addSubview:leftLabel];
        [bgView addSubview:self.phoneNumberLabel];
        
        bgView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bgViewClick)];
        [bgView addGestureRecognizer:tap];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 28 / WIDTH_5S_SCALE, SCREEN_WIDTH, 0.5)];
        lineView.backgroundColor = getColor(dividerColor);
        [_headView addSubview:lineView];
        
        UIView *line1View = [[UIView alloc]init];
        line1View.backgroundColor = getColor(dividerColor);
        
        UIView *line2View = [[UIView alloc]init];
        line2View.backgroundColor = getColor(dividerColor);
        
        UIView *line3View = [[UIView alloc]init];
        line3View.backgroundColor = getColor(dividerColor);
        
        UIView *line4View = [[UIView alloc]init];
        line4View.backgroundColor = getColor(dividerColor);
        
        [_headView sd_addSubviews:@[line1View , line2View , line3View , line4View , self.theOrderNumberLabel , self.theOrderNumber , self.logisticsInformationLabel , self.timeLabel , self.titleImageView , self.titleLabel , self.moneyLabel , bgView]];
        
        [self.titleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(10 / WIDTH_5S_SCALE);
            make.top.equalTo(lineView.mas_bottom).offset(10 / WIDTH_5S_SCALE);
            make.height.mas_equalTo(45 / WIDTH_5S_SCALE);
            make.width.mas_equalTo(45 / WIDTH_5S_SCALE);
        }];
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.titleImageView.mas_right).offset(10 / WIDTH_5S_SCALE);
            make.top.equalTo(lineView.mas_bottom).offset(0 / WIDTH_5S_SCALE);
            make.height.mas_equalTo(65 / WIDTH_5S_SCALE);
        }];
        
        [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-10 / WIDTH_5S_SCALE);
            make.top.equalTo(lineView.mas_bottom).offset(0 / WIDTH_5S_SCALE);
            make.height.mas_equalTo(65 / WIDTH_5S_SCALE);
        }];
        
        [line1View mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0 / WIDTH_5S_SCALE);
            make.right.mas_equalTo(0 / WIDTH_5S_SCALE);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(0 / WIDTH_5S_SCALE);
            make.height.mas_equalTo(0.5);
        }];
        
        [self.theOrderNumber mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(10 / WIDTH_5S_SCALE);
            make.top.equalTo(line1View.mas_bottom).offset(0 / WIDTH_5S_SCALE);
            make.height.mas_equalTo(27 / WIDTH_5S_SCALE);
        }];
        
        [self.theOrderNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-10 / WIDTH_5S_SCALE);
            make.top.equalTo(line1View.mas_bottom).offset(0 / WIDTH_5S_SCALE);
            make.height.mas_equalTo(27 / WIDTH_5S_SCALE);
        }];
        
        [line2View mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0 / WIDTH_5S_SCALE);
            make.right.mas_equalTo(0 / WIDTH_5S_SCALE);
            make.top.equalTo(self.theOrderNumber.mas_bottom).offset(0 / WIDTH_5S_SCALE);
            make.height.mas_equalTo(0.5);
        }];
        
        [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(10 / WIDTH_5S_SCALE);
            make.right.mas_equalTo(-10 / WIDTH_5S_SCALE);
            make.top.equalTo(line2View.mas_bottom).offset(0 / WIDTH_5S_SCALE);
            make.height.mas_equalTo(54 / WIDTH_5S_SCALE);
        }];
        
        [line3View mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0 / WIDTH_5S_SCALE);
            make.right.mas_equalTo(0 / WIDTH_5S_SCALE);
            make.top.equalTo(self.timeLabel.mas_bottom).offset(0 / WIDTH_5S_SCALE);
            make.height.mas_equalTo(0.5);
        }];
        
        [self.logisticsInformationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(10 / WIDTH_5S_SCALE);
            make.top.equalTo(line3View.mas_bottom).offset(0 / WIDTH_5S_SCALE);
            make.height.mas_equalTo(30 / WIDTH_5S_SCALE);
        }];
        
        [line4View mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0 / WIDTH_5S_SCALE);
            make.right.mas_equalTo(0 / WIDTH_5S_SCALE);
            make.top.equalTo(self.logisticsInformationLabel.mas_bottom).offset(0 / WIDTH_5S_SCALE);
            make.height.mas_equalTo(0.5);
        }];
        
    }
    return _headView;
}

- (UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel  = [[UILabel alloc]init];
        _timeLabel.font = DEF_FontSize_14;
        _timeLabel.textAlignment = NSTextAlignmentCenter;
        _timeLabel.textColor = getColor(textColor);
        _timeLabel.attributedText = [self getAttributeName:@"2017-02-01 11:22:33" secTime:@"2017-02-01 11:23:44"];
    }
    return _timeLabel;
}

- (NSAttributedString *)getAttributeName:(NSString *)oneTime secTime:(NSString *)secTime{
    NSMutableAttributedString *attStr = [NSMutableAttributedString new];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"下单时间                             %@",oneTime]]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"下单时间                             %@",secTime]]];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:6];//调整行间距
    [paragraphStyle setAlignment:NSTextAlignmentCenter];
    [attStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attStr.mutableString length])];
    
    return attStr;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel  = [[UILabel alloc]init];
        _titleLabel.font = DEF_FontSize_14;
        _titleLabel.textColor = getColor(textColor);
        _titleLabel.numberOfLines = 0;
        _titleLabel.attributedText = [self getAttributeName:@"名称" info:@"25" money:@"299"];
    }
    return _titleLabel;
}

- (NSAttributedString *)getAttributeName:(NSString *)name info:(NSString *)info money:(NSString *)money{
    NSMutableAttributedString *attStr = [NSMutableAttributedString new];
    
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:name]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"数量  "]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:info]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"价格  ¥"]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:money]];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:6];//调整行间距
    
    [attStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attStr.mutableString length])];
    
    return attStr;
}

- (UIImageView *)titleImageView{
    if (!_titleImageView) {
        _titleImageView = [UIImageView new];
        _titleImageView.contentMode = UIViewContentModeScaleAspectFit;
        _titleImageView.backgroundColor = getColor(@"666666");
        _titleImageView.image = [UIImage imageNamed:@""];
    }
    return _titleImageView;
}

- (UILabel *)moneyLabel{
    if (!_moneyLabel) {
        _moneyLabel  = [[UILabel alloc]init];
        _moneyLabel.font = DEF_FontSize_14;
        _moneyLabel.textColor = getColor(mainColor);
        _moneyLabel.text = [NSString stringWithFormat:@"总价  ¥%@",@"36.00"];
    }
    return _moneyLabel;
}

- (UILabel *)theOrderNumber{
    if (!_theOrderNumber) {
        _theOrderNumber  = [[UILabel alloc]init];
        _theOrderNumber.font = DEF_FontSize_14;
        _theOrderNumber.textColor = getColor(textColor);
        NSMutableAttributedString *attStr1 = [NSMutableAttributedString new];
        NSTextAttachment *seeAtt1 = [NSTextAttachment new];
        seeAtt1.bounds = CGRectMake(0, -2 / WIDTH_5S_SCALE, 10 / WIDTH_5S_SCALE, 13 / WIDTH_5S_SCALE);
        seeAtt1.image = [UIImage imageNamed:@"root_TheOrderNumber"];
        [attStr1 appendAttributedString:[NSAttributedString attributedStringWithAttachment:seeAtt1]];
        [attStr1 appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"  %@",@"订单号"]]];
        _theOrderNumber.attributedText = attStr1;
    }
    return _theOrderNumber;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = [UIView new];
        _tableView.tableHeaderView = self.headView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.backgroundColor = getColor(@"ffffff");
    }
    return _tableView;
}

#pragma mark - click

- (void)toApplyForARefundButtonClick{
    NSLog(@"申请退款");
}

- (void)bgViewClick{
    [self callPhone:self.phoneNumberLabel.text];
}

#pragma mark - <UITableViewDelegate , UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShopTheOrderDetailsTableViewCell *cell = [ShopTheOrderDetailsTableViewCell cellForTableView:tableView];
    cell.indexPath = indexPath;
    cell.model = self.dataArray[indexPath.row];
    //cell.accessoryType = indexPath.row == 3 ?  UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60 / WIDTH_5S_SCALE;
}

#pragma mark - data

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray  = @[@{@"title":@"卖家发货",@"time":@"2017-09-08 12:33:11"}.mutableCopy,
                        @{@"title":@"由广东物流公司发往沈阳中转站",@"time":@"2017-09-08 12:33:11"}.mutableCopy
                        ,
                        @{@"title":@"辽宁沈阳总部收入",@"time":@"2017-09-08 12:33:11"}.mutableCopy
                        ,
                        @{@"title":@"辽宁沈阳浑南第五分部已收入",@"time":@"2017-09-08 12:33:11",@"phone":@"15542914939"}.mutableCopy
                        ,
                        @{@"title":@"客户已经签收",@"time":@"2017-09-08 12:33:11"}.mutableCopy
                        ].mutableCopy;
    }
    return _dataArray;
}


@end
